package com.code.ui;


import com.code.generator.ProjectGenerator;
import com.code.generator.model.GeneratorConfiguration;
import com.code.ui.DaoCodeGeneratorPanel;
import java.awt.Color;
import java.awt.Component;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.SoftBevelBorder;
import javax.swing.filechooser.FileFilter;

public class ProjectGeneratorPanel extends JPanel {
    private static final long serialVersionUID = 1L;

    public ProjectGeneratorPanel(final GeneratorConfiguration projectConfiguration, final JFrame frame) {
        this.setLayout((LayoutManager)null);
        JLabel lblNewLabel = new JLabel("项目根路径");
        lblNewLabel.setBounds(28, 25, 121, 26);
        this.add(lblNewLabel);
        
        final JTextField projectDirTextField = new JTextField();
        projectDirTextField.setEditable(false);
        projectDirTextField.setColumns(10);
        projectDirTextField.setBounds(104, 28, 561, 26);
        this.add(projectDirTextField);
        projectDirTextField.setText(projectConfiguration.getRootProjectDirectory());
        
        JLabel lblProjectName = new JLabel("项目简称");
        lblProjectName.setBounds(28, 70, 121, 26);
        this.add(lblProjectName);
        
        final JTextField projectNameTextField = new JTextField();
        projectNameTextField.setBounds(104, 71, 561, 26);
        this.add(projectNameTextField);
        projectNameTextField.setColumns(10);
        projectNameTextField.setText(projectConfiguration.getProjectName());
        
        JLabel label_2 = new JLabel("默认依赖");
        label_2.setBounds(28, 261, 54, 15);
        this.add(label_2);
        final JCheckBox useMysqlChckbx = new JCheckBox("mysql");
        useMysqlChckbx.setSelected(true);
        useMysqlChckbx.setBounds(104, 257, 68, 23);
        this.add(useMysqlChckbx);
        final JCheckBox useOraclechckbx = new JCheckBox("oracle");
        useOraclechckbx.setBounds(190, 257, 68, 23);
        this.add(useOraclechckbx);
        final JCheckBox useRedisChckbx = new JCheckBox("redis");
        useRedisChckbx.setBounds(280, 257, 55, 23);
        this.add(useRedisChckbx);
        
        JLabel label_3 = new JLabel("特性");
        label_3.setBounds(28, 304, 54, 15);
        this.add(label_3);
        final JCheckBox needShardingCheckBox = new JCheckBox("分库分表");
        needShardingCheckBox.setBounds(104, 300, 78, 23);
        this.add(needShardingCheckBox);
        
        JLabel label_5 = new JLabel("业务组件");
        label_5.setBounds(28, 341, 78, 15);
        this.add(label_5);
        final JCheckBox needBaseUserCheckBox = new JCheckBox("基础账户");
        needBaseUserCheckBox.setBounds(104, 337, 78, 23);
        this.add(needBaseUserCheckBox);
        
        JPanel panel = new JPanel();
        panel.setBorder(new SoftBevelBorder(1, (Color)null, (Color)null, (Color)null, (Color)null));
        panel.setBounds(10, 10, 825, 212);
        this.add(panel);
        panel.setLayout((LayoutManager)null);
        JLabel label_1 = new JLabel("包前缀");
        label_1.setBounds(21, 99, 73, 26);
        panel.add(label_1);
        final JTextField basePackageNameTextField = new JTextField();
        basePackageNameTextField.setBounds(93, 100, 563, 26);
        panel.add(basePackageNameTextField);
        basePackageNameTextField.setColumns(10);
        basePackageNameTextField.setText(projectConfiguration.getBasePackage());
        JLabel label = new JLabel("项目编码");
        label.setBounds(21, 145, 54, 15);
        panel.add(label);
        final JComboBox encodingComboBox = new JComboBox();
        encodingComboBox.setBounds(93, 142, 79, 21);
        panel.add(encodingComboBox);
        encodingComboBox.setModel(new DefaultComboBoxModel(new String[]{"UTF-8", "GBK"}));
        JLabel label_4 = new JLabel("持久化框架");
        label_4.setBounds(21, 175, 73, 15);
        panel.add(label_4);
        JComboBox persistenceTypeComboBox = new JComboBox();
        persistenceTypeComboBox.setModel(new DefaultComboBoxModel(new String[]{"ibatis", "mybatis"}));
        persistenceTypeComboBox.setBounds(93, 173, 79, 21);
        panel.add(persistenceTypeComboBox);
        JButton selectFileBtn = new JButton("浏览");
        selectFileBtn.setBounds(673, 20, 119, 23);
        panel.add(selectFileBtn);
        selectFileBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                JFileChooser jfChooser = new JFileChooser(projectDirTextField.getText());//文件选择对话框框
                jfChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//仅显示目录
                jfChooser.setDialogTitle("选择项目生成到的根目录");
                jfChooser.setFileFilter(new FileFilter() {
                    public String getDescription() {
                        return "";
                    }

                    public boolean accept(File f) {
                        return f.isDirectory();
                    }
                });
                int result = jfChooser.showOpenDialog((Component)null);
                if(result == JFileChooser.APPROVE_OPTION) {//打开
                    projectDirTextField.setText(jfChooser.getSelectedFile().getPath().toString().replace("\\", "/"));
//                    jfChooser.hide();
                    jfChooser.setVisible(false);//关闭选择框
                }

            }
        });
        final JCheckBox useSafServerChckbx = new JCheckBox("saf服务端");
        useSafServerChckbx.setBounds(461, 257, 86, 23);
        this.add(useSafServerChckbx);
        final JCheckBox useZookeeperChckbx = new JCheckBox("zookeeper");
        useZookeeperChckbx.setBounds(677, 257, 86, 23);
        this.add(useZookeeperChckbx);
        final JCheckBox mqChckbx = new JCheckBox("MQ服务");
        mqChckbx.setBounds(368, 257, 103, 23);
        this.add(mqChckbx);
        projectDirTextField.grabFocus();
        final JCheckBox useSafClientChckbx = new JCheckBox("saf客户端");
        useSafClientChckbx.setBounds(567, 257, 84, 23);
        this.add(useSafClientChckbx);
        
        useMysqlChckbx.setSelected(projectConfiguration.isUseMysql());
        useOraclechckbx.setSelected(projectConfiguration.isUseOracle());
        useRedisChckbx.setSelected(projectConfiguration.isUseRedis());
        useSafServerChckbx.setSelected(projectConfiguration.isUseSafServer());
        useSafClientChckbx.setSelected(projectConfiguration.isUseSafClient());
        mqChckbx.setSelected(projectConfiguration.isUseMQ());
        useZookeeperChckbx.setSelected(useZookeeperChckbx.isSelected());
        needBaseUserCheckBox.setSelected(projectConfiguration.isNeedBaseUser());
        
        JLabel label_6 = new JLabel("生成信息");
        label_6.setBounds(28, 420, 78, 15);
        this.add(label_6);
        final JCheckBox generatorDomainCheckbox = new JCheckBox("生成Domain");
        generatorDomainCheckbox.setBounds(104, 416, 86, 23);
        this.add(generatorDomainCheckbox);
        generatorDomainCheckbox.setSelected(projectConfiguration.isGenerateDomain());
        final JCheckBox generatorSqlMapCheckbox = new JCheckBox("生成SqlMap配置文件");
        generatorSqlMapCheckbox.setBounds(280, 416, 148, 23);
        this.add(generatorSqlMapCheckbox);
        generatorSqlMapCheckbox.setSelected(projectConfiguration.isGenerateSqlMap());
        final JCheckBox generatorDaoCheckbox = new JCheckBox("生成DAO");
        generatorDaoCheckbox.setBounds(190, 416, 93, 23);
        this.add(generatorDaoCheckbox);
        generatorDaoCheckbox.setSelected(projectConfiguration.isGenerateDao());
        
        JLabel label_7 = new JLabel("业务模块");
        label_7.setBounds(28, 379, 68, 15);
        this.add(label_7);
        final JCheckBox useRpcChckbx = new JCheckBox("RPC");
        useRpcChckbx.setBounds(104, 375, 86, 23);
        this.add(useRpcChckbx);
        useRpcChckbx.setSelected(projectConfiguration.isUseRpcModule());
        final JCheckBox useISRpcChckbx = new JCheckBox("ISRPC");
        useISRpcChckbx.setBounds(190, 375, 68, 23);
        this.add(useISRpcChckbx);
        useISRpcChckbx.setSelected(projectConfiguration.isUseIsRpcModule());
        final JCheckBox useWorkChckbx = new JCheckBox("WORK");
        useWorkChckbx.setBounds(280, 375, 103, 23);
        this.add(useWorkChckbx);
        useWorkChckbx.setSelected(projectConfiguration.isUseWorkModule());
        
        final JButton button = new JButton("生成项目");
        button.setBounds(104, 473, 92, 26);
        this.add(button);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                projectConfiguration.setRootProjectDirectory(projectDirTextField.getText());
                projectConfiguration.setBasePackage(basePackageNameTextField.getText());
                projectConfiguration.setProjectName(projectNameTextField.getText());
                projectConfiguration.setEncoding(encodingComboBox.getSelectedItem().toString());
                projectConfiguration.setUseMysql(useMysqlChckbx.isSelected());
                projectConfiguration.setUseOracle(useOraclechckbx.isSelected());
                projectConfiguration.setUseRedis(useRedisChckbx.isSelected());
                projectConfiguration.setUseSafServer(useSafServerChckbx.isSelected());
                projectConfiguration.setUseSafClient(useSafClientChckbx.isSelected());
                projectConfiguration.setUseMQ(mqChckbx.isSelected());
                projectConfiguration.setUseZookeeper(useZookeeperChckbx.isSelected());
                projectConfiguration.setNeedSharding(needShardingCheckBox.isSelected());
                projectConfiguration.setNeedBaseUser(needBaseUserCheckBox.isSelected());
                projectConfiguration.setGenerateDomain(generatorDomainCheckbox.isSelected());
                projectConfiguration.setGenerateSqlMap(generatorSqlMapCheckbox.isSelected());
                projectConfiguration.setGenerateDao(generatorDaoCheckbox.isSelected());
                projectConfiguration.setUseRpcModule(useRpcChckbx.isSelected());
                projectConfiguration.setUseIsRpcModule(useISRpcChckbx.isSelected());
                projectConfiguration.setUseWorkModule(useWorkChckbx.isSelected());
                button.setEnabled(false);

                try {
                    (new ProjectGenerator()).generate(projectConfiguration);
                    JOptionPane.showMessageDialog((Component)null, "生成成功了！");
                } catch (Exception var3) {
                    var3.printStackTrace();
                    JOptionPane.showMessageDialog((Component)null, "生成失败了：" + var3.getMessage());
                }

                button.setEnabled(true);
            }
        });
        final JButton btndao = new JButton("生成DAO");
        btndao.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                projectConfiguration.setRootProjectDirectory(projectDirTextField.getText());
                projectConfiguration.setBasePackage(basePackageNameTextField.getText());
                projectConfiguration.setProjectName(projectNameTextField.getText());
                projectConfiguration.setEncoding(encodingComboBox.getSelectedItem().toString());
                projectConfiguration.setUseMysql(useMysqlChckbx.isSelected());
                projectConfiguration.setUseOracle(useOraclechckbx.isSelected());
                projectConfiguration.setUseRedis(useRedisChckbx.isSelected());
                projectConfiguration.setUseSafServer(useSafServerChckbx.isSelected());
                projectConfiguration.setUseSafClient(useSafClientChckbx.isSelected());
                projectConfiguration.setUseMQ(mqChckbx.isSelected());
                projectConfiguration.setUseZookeeper(useZookeeperChckbx.isSelected());
                projectConfiguration.setNeedSharding(needShardingCheckBox.isSelected());
                projectConfiguration.setNeedBaseUser(needBaseUserCheckBox.isSelected());
                projectConfiguration.setGenerateDomain(generatorDomainCheckbox.isSelected());
                projectConfiguration.setGenerateSqlMap(generatorSqlMapCheckbox.isSelected());
                projectConfiguration.setGenerateDao(generatorDaoCheckbox.isSelected());
                projectConfiguration.setUseRpcModule(useRpcChckbx.isSelected());
                projectConfiguration.setUseIsRpcModule(useISRpcChckbx.isSelected());
                projectConfiguration.setUseWorkModule(useWorkChckbx.isSelected());
                btndao.setEnabled(false);

                try {
                    frame.getContentPane().removeAll();
                    DaoCodeGeneratorPanel e1 = new DaoCodeGeneratorPanel(projectConfiguration, frame);
                    frame.getContentPane().add(e1);
                    frame.getContentPane().invalidate();
                    frame.getContentPane().repaint();
                    frame.getContentPane().validate();
                } catch (Exception var3) {
                    JOptionPane.showMessageDialog((Component)null, "生成失败了：" + var3.getMessage());
                }

                btndao.setEnabled(true);
            }
        });
        btndao.setBounds(405, 473, 92, 26);
        this.add(btndao);
        
        JButton exitButton = new JButton("退出");
        exitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        exitButton.setBounds(671, 473, 92, 26);
        this.add(exitButton);
    }
}

